/**
 * A non-blocking array iterator.
 */
(function() {
    // {{{ Contstructor
    /**
     *
     * @access public
     * @param int interval
     * @param object a set of delegations.
     * @return object jQuery chain.
     */
    translator.iterator = function (interval, limit) {
        this.interval = interval || 100;
        this.limit = limit || 10;
        this.timer = null;
        this.queue = [];

        return this;
    };
    // }}}
    // {{{ Methods
    translator.iterator.prototype = { 
        // {{{ start()
        start : function () {
            var self = this;
            self.process();
            if (null === this.timer) {
                this.timer = setInterval(function () {
                    self.process.apply(self);
                }, this.interval);
            }
        },
        // }}}
        // {{{ stop()
        stop : function () {
            clearInterval(this.timer);
            this.timer = null;
        },
        // }}}
        // {{{ process()
        process : function () {
            var batch = this.queue.slice(0, this.limit);

            for (var i = 0, l = batch.length; i < l; i++) {
                this.callback.call(batch[i]);
            }

            this.queue.splice(0, this.limit);

            if (0 === this.queue.length) {
                this.stop();  
            }
        },
        // }}}
        // {{{ add(data, fn)
        add : function (data, fn) {
            if (data instanceof Array) {
                this.queue = this.queue.concat(data);
            } else {
                this.queue.push(data);
            }

            if (typeof fn === 'function') {
                this.callback = fn;
            }

            this.start();
        },
        // }}}
        // {{{ reset()
        reset : function () {
            this.stop();
            this.queue = [];
        }
        // }}}
    };
    // }}}
})();
